﻿using Max.Ph5_2009A.NewShopping.API.Pay;
using Max.Ph5_2009A.NewShopping.DTO;
using Max.Ph5_2009A.NewShopping.EFCore;
using Max.Ph5_2009A.NewShopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.Ph5_2009A.NewShopping.API.Controllers.Order
{
    /// <summary>
    /// 订单控制器
    /// 2023/4/7
    /// </summary>
    [Route("order")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        IOrderMasterInfoService _service;

        public OrderController(IOrderMasterInfoService service)
        {
            _service = service;
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateOrder(CreateOrderInputDto dto)
        {
            ResponseResultOutputDto resutl = new();
            //第一件：判断参数
            if(string.IsNullOrEmpty( dto.OrderMaster.RecivedName))
            {
                resutl.Code = Domain.Shared.ResponseResultStateCode.系统错误;
                resutl.Message = "参数无效";
                return BadRequest(resutl);
            }

            //调用业务处理
            var s = _service.CreateOrder(dto);

            //统一返回值
            resutl.Data = s;

            return Ok(resutl);
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        [HttpPost("success")]
        public IActionResult PaySuccess(string? orderNo)
        {
            ResponseResultOutputDto dto = new();

            IPay pay = new AlipayPay();
            var result = pay.Pay(new AliPayParams() { 
                OutTradeNo = System.DateTime.Now.ToString("yyyyMMddHHssmm"), 
                TotalAmount = 0.01 }
            );
            dto.Data = new { state = result.Item1, body = result.Item2 };

            return Ok(dto);
        }
    }
}
